(function($){
    function buildMenu(target){
        var state = $(target).data('datagrid');
        if (!state.columnMenu){
            state.columnMenu = $('<div></div>').appendTo('body');
            state.columnMenu.menu({
                onClick: function(item){
                    if (item.iconCls == 'tree-checkbox1'){
                        $(target).datagrid('hideColumn', item.name);
                        $(this).menu('setIcon', {
                            target: item.target,
                            iconCls: 'tree-checkbox0'
                        });
                    } else {
                        $(target).datagrid('showColumn', item.name);
                        $(this).menu('setIcon', {
                            target: item.target,
                            iconCls: 'tree-checkbox1'
                        });
                    }
                }
            })
            var fields = $(target).datagrid('getColumnFields',true).concat($(target).datagrid('getColumnFields',false));
            debugger;
            for(var i=0; i<fields.length; i++){
                var field = fields[i];
                if(field !="_expander"){//屏蔽  '+'扩展展开 列
                    var col = $(target).datagrid('getColumnOption', field);
                    if(!col.checkbox && !col.hidden){ //屏蔽 checkbox 列,隐藏列
                    	state.columnMenu.menu('appendItem', {
                            text: col.title,
                            name: field,
                            iconCls: 'tree-checkbox1'
                        });
                    }
                }
            }
        }
        return state.columnMenu;
    }
    $.extend($.fn.datagrid.methods, {
        columnMenu: function(jq){
            return buildMenu(jq[0]);
        }
    });
})(jQuery);
